<?php
/**
 * 格式化表情
 * @param 
 * @return
 * @author fucaixie
 * @package /application/common/
 */

class Emotion
{	//获取表情数据
	
	private function getEmotions()
	{
		$emotionsstr=file_get_contents("js/iweibo/emotions.js");
		preg_match("/\\\"(.*?)\\\"/",$emotionsstr,$e);
		return $e[1];
		return $emotionsstr;
	}
	//格式化表情为数组格式
	private function getEmotionsArr($s)
	{	$returnArray=array();
		foreach(explode(",",$s) as $e)
		{$faceArr=array();
		 $f=explode("|",$e);
		 $faceArr["id"]=str_replace("f","",$f[0]);
		 $faceArr["name"]=$f[1];
		 $returnArray[]=$faceArr;	
		}
		return $returnArray;
	}	
	//替换表情	
	private function replaceEmotions($eArr,$str)
	{
		foreach($eArr as $e)
		{
		$str=str_replace("/".$e["name"],"<img alt=\"".$e["name"]."\" src=\"style/images/emotions/".$e["id"].".gif\" valign=\"top\"/>",$str);
		}
		return $str;
	}
	
	public function replace($str) 
	{	
		$emotionsstr=self::getEmotions();
		$emotionsArr=self::getEmotionsArr($emotionsstr);
		return self::replaceEmotions($emotionsArr,$str);
	}
}
?>